package com.wx.mall.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.goods.entity.Category;
import com.wx.mall.goods.entity.CategoryVo;
import com.wx.mall.goods.mapper.CategoryMapper;
import com.wx.mall.goods.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类表
 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    @Override
    public List<Category> getListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name","%Category%");
        queryWrapper.orderByAsc("name");
        return this.baseMapper.selectList(null);
    }


    @Override
    public List<Category> getChildrenAll(Integer pageNum, Integer pageSize,Integer  categoryParentId){
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("category_parent_id",categoryParentId);
        List<Category> list=this.list(wrapper);
        return list;
    };

    @Override
    public List<Category> getListByLevelPage(Integer pageNum, Integer pageSize, Integer categoryLevel) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("category_level",categoryLevel);
        List<Category> list=this.list(wrapper);
        return list;
    }

    @Override
    public Category getListAllById(Integer  categoryId) {

//ss
        Category list=this.getById(categoryId);
        return list;
    }

    @Override
    public List<Category> getListByParent(Integer id) {
        return this.list(new QueryWrapper<Category>().eq("category_parent_id",id));
    }

    ;


    @Override
    public int updateParentId(Category category) {
        boolean update=this.updateById(category);
        return update?0:1;
    }


    @Override
    public String getCategoryName(Integer  categoryId) {

        Category list=this.getById(categoryId);
        return list.getCategoryName();
    };

    @Override
    public List<String> getCategoryNameAll() {

        List<Category> list=this.list();
        List<String> categoryName=new ArrayList<>();
        for (Category category:list){
            categoryName.add(category.getCategoryName());
        }
        return categoryName;
    }

    @Override
    public List<Category> getCategoryAll() {

        List<Category> list=this.list();


        return list;
    }

    @Override
    public Category getCategoryValue(String categoryName) {

        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("category_name",categoryName);
        Category list=this.getOne(wrapper);

        return list;
    }

    @Override
    public int getCategoryLevel(Integer categoryId) {
        Category list=this.getById(categoryId);
        return list.getCategoryLevel();
    }

    @Override
    public int setForm(Category category) {
        boolean inset=this.save(category);
        //0为成功1为失败
        return inset?0:1;
    };


    @Override
    public List<Category> getListByLevel(Integer categoryLevel) {
        List<Category> listAll = this.list(new QueryWrapper<Category>().eq("category_level",categoryLevel));
        return listAll;
    };


    @Override
    public List<Category> getListBySort(Integer Sort) {
        List<Category> listAll;
//        0降1升
        if (Sort==0){
            listAll = this.list(new QueryWrapper<Category>().orderBy(true,false,"category_sort"));
        }else {
            listAll = this.list(new QueryWrapper<Category>().orderBy(true,true,"category_sort"));
        }
        return listAll;

    }

    @Override
    public List<CategoryVo> tree() {
        // 查询所有分类
        List<Category> list = this.baseMapper.selectList(null);
        // 类型转换
        List<CategoryVo> datas = JSON.parseArray(JSON.toJSONString(list),CategoryVo.class);
        // 结果集
        List<CategoryVo> result = new ArrayList<>();

        for (CategoryVo categoryVo : datas) {
            if (categoryVo.getCategoryLevel() == 1) {
                // 获取子分类
                this.getChildren(categoryVo,datas);
                result.add(categoryVo);
            }
        }

        return result;
    }


    /**
     * 递归查询子类
     * @param categoryVo    当前分类对象
     * @param list          所有分类
     * @return
     */
    private CategoryVo getChildren(CategoryVo categoryVo, List<CategoryVo> list) {
        if (categoryVo == null ) {
            categoryVo = new CategoryVo();
        }
        if (list == null || list.isEmpty()) {
            return categoryVo;
        }

        List<CategoryVo> children = new ArrayList<>();
        for (CategoryVo vo : list) {
            if (vo.getCategoryParentId() != null
                    && vo.getCategoryParentId().equals(categoryVo.getCategoryId())) {
                children.add(vo);
                // 判断是否3级分类，避免无效递归（3级分类没有子类）
                if (vo.getCategoryLevel() != null && vo.getCategoryLevel() < 3) {
                    this.getChildren(vo,list);
                }
            }
        }
        categoryVo.setChildList(children);
        return categoryVo;
    };

    @Override
    public int deleteList(Integer categoryId) {
        boolean sucess=this.removeById(categoryId);
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("category_parent_id",categoryId);
        boolean sucessTwo=this.remove(wrapper);
        int result;
        if(sucess==true||sucessTwo==true){
            result=1;
        }else {
            result=0;
        }
        return result;
    }
}
